package com.cx.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cx.mapper.LolBattleTeamMapper;
import com.cx.po.LolBattleTeamPo;
import com.cx.service.LolBattleTeamService;
import com.cx.vo.LolBattleTeamVo;
import com.cx.vo.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 残星
 * @Description LOL-对局队伍信息 业务逻辑类
 * @date 2022-11-30
 */
@Service
public class LolBattleTeamServiceImpl extends ServiceImpl<LolBattleTeamMapper, LolBattleTeamPo> implements LolBattleTeamService {
    @Resource(name = "mongoTemplate")
    private MongoTemplate mongoTemplate;
    @Resource(name = "lolBattleTeamMapper")
    private LolBattleTeamMapper lolBattleTeamMapper;

    @Override
    public Page<LolBattleTeamPo> findLolTeamPage(Page<LolBattleTeamPo> page) {
        return lolBattleTeamMapper.findLolTeamPage(page);
    }

    @Override
    public ResultVo<Object> updateLolBattleTeamDocument(LolBattleTeamVo lolBattleTeamVo) {
        ResultVo<Object> resultVo = new ResultVo<>();
        if (StrUtil.isEmpty(lolBattleTeamVo.getId())) {
            resultVo.result("500", "MongoDB主键不能为空！");
            return resultVo;
        }
        LolBattleTeamPo lolBattleTeamPo = new LolBattleTeamPo();
        BeanUtils.copyProperties(lolBattleTeamVo, lolBattleTeamPo);
        try {
            mongoTemplate.save(lolBattleTeamPo);
            resultVo.setSuccess(true);
            resultVo.result("0", "修改对局记录成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.result("500", "修改对局记录失败！");
        }
        return resultVo;
    }

    @Override
    public ResultVo<Object> deleteLolBattleTeam(Long gameId) {
        ResultVo<Object> resultVo = new ResultVo<>();
        if (gameId == null || gameId == 0L) {
            resultVo.result("500", "对局ID不能为空！");
            return resultVo;
        }
        try {
            Query queryIdList = Query.query(Criteria.where("gameId").in(gameId));
            List<LolBattleTeamPo> teamPoList = mongoTemplate.findAllAndRemove(queryIdList, LolBattleTeamPo.class);
            resultVo.setSuccess(true);
            resultVo.result("0", "删除对局队伍记录成功！", teamPoList);
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.result("500", "删除对局队伍记录失败！");
        }
        return resultVo;
    }

    @Override
    public ResultVo<Object> deleteLolBattleTeam(Long gameId, String id) {
        ResultVo<Object> resultVo = new ResultVo<>();
        if (gameId == null || gameId == 0L || StrUtil.isEmpty(id)) {
            resultVo.result("500", "对局ID和MongoDB ID不能同时为空！");
            return resultVo;
        }
        try {
            Query queryIdList = Query.query(Criteria.where("gameId").is(gameId).and("id").is(id));
            List<LolBattleTeamPo> teamPoList = mongoTemplate.findAllAndRemove(queryIdList, LolBattleTeamPo.class);
            resultVo.setSuccess(true);
            resultVo.result("0", "删除对局队伍记录成功！", teamPoList);
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.result("500", "删除对局队伍记录失败！");
        }
        return resultVo;
    }
}